<template>
	<view>
		<view class="wrap-list">
			<scroll-view
				scroll-y 
				style="height: 100%;width: 100%;"
				:refresher-enabled="true"
				:refresher-triggered="triggered" 
				refresher-background="#F3F5F9"
				@refresherrefresh="onRefresh" 
				@refresherrestore="onRestore" 
				@scrolltolower="reachBottom"
			>
				<view v-if="list.length > 0">
					<view class="presdete_top">
						<view class="presdete_top_item word-box-hover d-s-c" v-for="(item, index) in list" :key="index" @tap="wordClick(index)">
							<view class="ww40 f36 fb">
								{{ item.word }}
							</view>
							<view class="ww40 f36 fb">
								<view v-if="(currentIndex == index && clickTimes == 2) || item.check">{{ item.chinese }}</view>
							</view>
							<view class="ww20 d-e-c">
								<view class="presdete_top_item_box1 d-c-c mr20" :class="{'checked':item.check==1}" @tap.stop="wordCheck(index, 1)">
									<u-icon name="checkmark" color="#ffffff" size="36"></u-icon>
								</view>
								<view class="presdete_top_item_box2 d-c-c" :class="{'checked':item.check==2}" @tap.stop="wordCheck(index, 2)">
									<u-icon name="close" color="#ffffff" size="36"></u-icon>
								</view>
							</view>
						</view>
					</view>
					<u-loadmore :status="loadStatus" />
				</view>
				<u-empty v-else mode="data"></u-empty>
			</scroll-view>
		</view>
		<view class="presdete_bot d-b-c tc">
			<view class="presdete_bo" style="">
				<view class="">
					<u-badge :count="count" :offset="[-20, -30]"></u-badge>
				</view>
				<view class="f30 gray9">已选</view>
			</view>
			<view class="presdete_bot_but" @tap="startStudy()">开始识记</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: getApp().globalData.imgUrl,
				studyId: 0,
				count: 0,
				ids: '',
				knowIds: '',
				list: [],
				page: 1,
				pageSize: 10,
				lastPage: 1,
				loadStatus: 'loadmore',
				triggered: false,
				freshing: false,
				currentIndex: -1,
				clickTimes: 0,
			}
		},
		onLoad(option) {
			if (!this.$store.state.isLogin) {
				uni.$u.toast('登录超时请重新登录')
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login',
					});
				}, 1500)
				return false
			} else {
				if (option.studyId) {
					this.studyId = option.studyId
				}
				this.getCourseWordsList(this.studyId, this.page, this.pageSize)
			}
		},
		methods: {
			getCourseWordsList(studyId, page, pageSize) {
				this.$u.post(this.$u.api.gocourseWords, {
					study_id: studyId,
					page: page,
					page_size: pageSize
				}).then((res) => {
					let data = res.data
					this.page = data.current_page
					this.lastPage = data.last_page
					this.pageSize = data.per_page
					if (this.page == 1) {
						this.list = []
					}
					for (let i=0;i<data.data.length;i++) {
						let temp = data.data[i]
						temp.show = false
						temp.check = 0
						this.list.push(temp)
					}
					if (data.last_page <= data.current_page) {
						this.loadStatus = 'nomore'
					} else {
						this.loadStatus = 'loadmore'
					}
				}).catch((res) => {
					console.log(res)
				})
			},
			onRefresh() {
				if (this.freshing) {
					return false
				}
				this.freshing = true
				if (!this.triggered) {
					this.triggered = true
				}
				setTimeout(() => { 
					this.getCourseWordsList(this.studyId, 1, this.pageSize)
					this.triggered = false;//触发onRestore，并关闭刷新图标
					this.freshing = false;
				}, 1500) 
			},
			onRestore() { 
				console.log("onRestore")
			}, 
			reachBottom() {
				setTimeout(() => {
					if (this.page < this.lastPage) {
						this.loadStatus = 'loading'
						setTimeout(() => {
							this.getCourseWordsList(this.studyId, this.page + 1, this.pageSize)
						}, 1000)
					} else {
						this.loadStatus = 'nomore'
					}
				}, 1000)
			},
			// 点击单词
			wordClick(i) {
				if (this.currentIndex == i) {
					if (this.clickTimes == 0) {
						this.clickTimes = 1
					} else if (this.clickTimes == 1) {
						this.clickTimes = 2
					} else if (this.clickTimes == 2) {
						this.clickTimes = 0
					} else {
						return false
					}
				} else {
					this.clickTimes = 1
				}
				this.currentIndex = i
				// this.list[i].show = true
				// 获取单词音频
				this.$u.post(this.$u.api.wordVoice, {
					word: this.list[i].word,
					type: this.$store.state.voiceType
				}).then((res) => {
					this.utils.playAudio(this.imgUrl + res.data)
				}).catch((err) => {
					console.log(err)
					uni.$u.toast(err.data.msg)
				})
			},
			// 选择单词
			wordCheck(i, n) {
				if (this.list[i].check == n) {
					this.list[i].check = 0
				} else {
					this.list[i].check = n
					if(i < this.list.length - 1) {
						let audio = this.list[i+1].audio_url
						this.playAudio(audio)
					}
				}
				let count = 0
				let idArray = []
				let knowIdArray = []
				for (let j=0;j<this.list.length;j++) {
					let words = this.list[j]
					if (words.check == 1) {
						knowIdArray.push(words.id)
					}
					if (words.check == 2) {
						count += 1
						idArray.push(words.id)
					}
				}
				this.count = count
				this.ids = idArray.join(',')
				this.knowIds = knowIdArray.join(',')
			},
			// 开始识记
			startStudy() {
				if (this.count == 0 || this.ids == '') {
					uni.$u.toast('请选择需要识记的单词')
					return false
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$u.post(this.$u.api.readdStudy, {
					study_id: this.studyId,
					course_words_ids: this.ids,
					course_words_total: this.count,
					course_words_know_ids: this.knowIds,
				}).then((res) => {
					uni.hideLoading()
					uni.navigateTo({
						url: '/pages/indexhome/word?studyId=' + res.data
					})
				}).catch((res) => {
					uni.hideLoading()
					console.log(res)
					uni.$u.toast(res.data.msg)
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	::v-deep .u-empty {
		height: 80vh !important;
	}
	
	.presdete_top {
		// padding-bottom: 20rpx;
	}

	.presdete_top_item {
		width: 92%;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: 20rpx auto;
		padding: 45rpx 30rpx;
		box-sizing: border-box;
	}
	.presdete_top_item_box1,.presdete_top_item_box2 {
		width: 50rpx;
		height: 50rpx;
		background: linear-gradient(250deg, #999999, #C8C4C4);
		border-radius: 50%;
	}
	.presdete_top_item_box1.checked {
		background: linear-gradient(250deg, #5BA9FF, #2B87F7) !important;
	}

	.presdete_top_item_box2.checked {
		background: linear-gradient(250deg, #F85548, #F77B2D) !important;
	}

	.presdete_bot {
		width: 100%;
		background-color: #ffffff;
		height: 12vh;
		padding: 0 54rpx;
		position: absolute;
		bottom: 0;
		left: 0;
		z-index: 999;
	}
	.presdete_bot_but{
		width: 216rpx;
		height: 7vh;
		background: linear-gradient(90deg, #1F93FF, #387BEF);
		border-radius: 40rpx;
		font-weight: bold;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 7vh;
	}
	.presdete_bo{
		position: relative
	}
</style>
